package com.mybatis.lo.builder;

import com.mybatis.lo.config.Configuration;
import com.mybatis.lo.config.MappedStatement;
import com.mybatis.lo.sqlsource.SqlSource;
import org.dom4j.Element;

/**
 * 解析`<select`标签整体配置信息的构建器
 * @author zhangbh
 * 2023-06-16 07:33
 **/
public class XMLStatementBuilder extends BaseBuilder {
    public XMLStatementBuilder(Configuration configuration) {
        super(configuration);
    }

    public void loadStatementNode(String namespace, Element element) {
        String id = element.attributeValue("id");
        String resultType = element.attributeValue("resultType");
        String statementType = element.attributeValue("statementType");
        // 加载SqlSource
        SqlSource sqlSource = createSqlSource(element);
        MappedStatement mappedStatement = new MappedStatement(id, statementType, sqlSource, resultType);
        configuration.addMappedStatement(namespace + "." + id, mappedStatement);
    }

    private SqlSource createSqlSource(Element element) {
        return new XMLScriptBuilder(configuration).loadScriptNode(element);
    }
}
